package com.hxs.learn.config.web;

import com.hxs.learn.common.web.constants.BaseWebConst;
import com.hxs.learn.env.EnvContextConfig;
import com.hxs.learn.consts.PropertiesKeys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import javax.servlet.ServletContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author HuXinsheng
 * @since 2018/2/23
 */
@Configuration
public class AdminWebMvcConfiguration implements ServletContextInitializer {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void onStartup(ServletContext servletContext) {
        servletContext.setAttribute(BaseWebConst.KEY_TITLE, EnvContextConfig.get(PropertiesKeys.APP_TITLE));
    }

    @Bean
    public TomcatServletWebServerFactory containerCustomizer() {
        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
        ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
        Set<ErrorPage> errorPages = new HashSet<>();
        errorPages.add(error404Page);
        errorPages.add(error500Page);
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setErrorPages(errorPages);
        return factory;
    }

}
